package com.ningmengs789.video_shooting.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.view.View;
import android.view.WindowManager;

import com.ningmengs789.video_shooting.R;
import com.ningmengs789.video_shooting.base.BaseActivity;
import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.cjt2325.cameralibrary.listener.RecordStateListener;
import com.cjt2325.cameralibrary.util.FileUtil;

import java.io.File;

public class XVideoCameraActivity extends BaseActivity {

    private JCameraView mJCameraView;
    private long mTime = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_camera;
    }

    @Override
    protected void init() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    protected void initView() {

        mJCameraView = findViewById(R.id.jcameraview);

        //设置视频保存路径
        mJCameraView.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "videoeditor" + File.separator + "small_video");
        mJCameraView.setMinDuration(1000);
        mJCameraView.setFeatures(JCameraView.BUTTON_STATE_BOTH);
        mJCameraView.setTip(getString(R.string.video_shooting_5));
        mJCameraView.setRecordShortTip(getString(R.string.video_shooting_6));
        mJCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_POOR);
        mJCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                //错误监听
                Intent intent = new Intent();
                setResult(10001, intent);
                finish();
            }

            @Override
            public void AudioPermissionError() {
                //权限未设置
            }
        });

        //JCameraView监听
        mJCameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmap
                String path = FileUtil.createFile(bitmap);
                Intent intent = new Intent();
                intent.putExtra("imgPath", path);
                setResult(10002, intent);
                finish();

                //Toast.makeText(XVideoCameraActivity.this, path, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void recordSuccess(String videoPath, Bitmap bitmap) {
                //获取视频路径
                String path = FileUtil.createFile(bitmap);
                Intent intent = new Intent();
                intent.putExtra("videoPath", videoPath);
                intent.putExtra("videoTime", mTime);
                setResult(10003, intent);
                finish();

                //Toast.makeText(XVideoCameraActivity.this, videoPath + "时长》》" + mTime, Toast.LENGTH_SHORT).show();
            }
        });
        mJCameraView.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                finish();
            }
        });

        mJCameraView.setRightClickListener(new ClickListener() {
            @Override
            public void onClick() {
                //Toast.makeText(XVideoCameraActivity.this, "Right", Toast.LENGTH_SHORT).show();
            }
        });

        mJCameraView.setRecordStateListener(new RecordStateListener() {
            @Override
            public void recordStart() {

            }

            @Override
            public void recordEnd(long time) {
                mTime = time;
            }

            @Override
            public void recordCancel() {

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        //全屏显示
        if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } else {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mJCameraView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mJCameraView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

